﻿<!doctype html>
<!--<html manifest="drawCache.manifest">-->
<head>

<meta charset="utf-8" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name=​"apple-touch-fullscreen" content=​"yes">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1 maximum-scale=1">
<link rel="apple-touch-icon" href="game_icon.png"/>

<style type="text/css">
	html, body { border: 0; margin: 0; height: 100%; height: 100%;}
	canvas { display: block; background-color: #02f;}

</style>

</head>

<title> Speed </title>


<script src = "./js/game.js"></script>
<script src = "./js/gl.js"></script>
<script src = "./js/shader.js"></script>
<script src = "./js/math.js"></script>
<script src = "./js/util.js"></script>
<script src = "./js/box.js"></script>
<script src = "./js/lzma.js"></script>
<script src = "./js/ctm.js"></script>
<script src = "./js/model.js"></script>
<script src = "./js/poly.js"></script>
<script src = "./js/image.js"></script>
<script src = "./js/camera.js"></script>
<script src = "./js/sky.js"></script>
<script src = "./js/water.js"></script>

<!----------------------shader--------------------------->
<!--------- test ---------------------------------------->
<script id="vsCar" type="shader/vertex">
	#ifdef GL_ES
	precision highp float;
	#endif

	attribute vec3 aPos;
	attribute vec3 aNor;

	uniform mat4 matMVP;

	varying vec3 nor;
	void main(void)
	{
		gl_Position = matMVP * vec4(aPos, 1.0);
		nor = aNor;
	}

</script>

<script id="psCar" type="shader/fragment">

	#ifdef GL_ES
	precision highp float;
	#endif

	uniform float time;
	uniform vec2 resolution;
	uniform vec2 mouse;

	uniform sampler2D sampler;

	varying vec3 nor;
	void main(void)
	{
		float color = dot(nor, vec3(1,0,0));
		gl_FragColor = vec4(color, color, color, 1.0);
	}

</script>

<script id="vsModel" type="shader/vertex">
	#ifdef GL_ES
	precision highp float;
	#endif

	attribute vec3 aPos;
	attribute vec3 aNor;
	attribute vec2 aTc;

	uniform mat4 matMVP;

	varying vec2 tc;
	varying vec3 nor;
	void main(void)
	{
		gl_Position = matMVP * vec4(aPos, 1.0);
		//nor = aNor;
		tc = vec2(aTc.x, -aTc.y);
	}

</script>

<script id="psModel" type="shader/fragment">

	#ifdef GL_ES
	precision highp float;
	#endif

	uniform float time;
	uniform vec2 resolution;
	uniform vec2 mouse;

	uniform sampler2D sampler;

	varying vec2 tc;
	varying vec3 nor;
	void main(void)
	{
		//float d = dot(nor, vec3(0, 0, 1));
		gl_FragColor = texture2D(sampler, tc);
	}

</script>

<!---------  icePoly  ---------------------------------------->
<script id="vsIcePoly" type="shader/vertex">
	#ifdef GL_ES
	precision highp float;
	#endif

	attribute vec3 aPos;
	attribute vec3 aNor;
	attribute vec2 aTc;

	uniform mat4 matMVP;

	varying vec2 tc;
	void main(void)
	{
		gl_Position = matMVP * vec4(aPos, 1.0);
		tc = aTc;
	}

</script>

<script id="psIcePoly" type="shader/fragment">

	#ifdef GL_ES
	precision highp float;
	#endif

	uniform float time;
	uniform vec2 resolution;
	uniform vec2 mouse;

	uniform sampler2D sampler;

	varying vec2 tc;
	void main(void)
	{
		gl_FragColor = texture2D(sampler, tc);
	}

</script>

<!------------------- SkyBox -------------------------------->

<!------------------- SkyBox -------------------------------->
<script id="vsSkybox" type="shader/vertex">
	#ifdef GL_ES
	precision highp float;
	#endif

	attribute vec3 aPos;

	uniform mat4 matIVP;

	varying vec3 tc;
	void main(void)
	{
		gl_Position = vec4(aPos, 1.0);
		tc = (matIVP * vec4(aPos, 1.0)).xyz;
		tc = normalize(tc);
	}

</script>

<script id="psSkybox" type="shader/fragment">

	#ifdef GL_ES
	precision highp float;
	#endif

	uniform samplerCube sampler;

	varying vec3 tc;
	void main(void)
	{
		gl_FragColor = textureCube(sampler, tc);
	}

</script>



<!------------------- IceGround -------------------------------->
<script id="vsIce" type="shader/vertex">
	#ifdef GL_ES
	precision highp float;
	#endif

	attribute vec3 aPos;

	uniform mat4 matMVP;
	uniform vec3 camPos;

	varying vec2 tc;
	varying vec3 tcCube;
	varying vec3 eyeDir;

	void main(void)
	{
		gl_Position = matMVP * vec4(aPos, 1.0);
		tc = aPos.xz*0.02;
		eyeDir = camPos - aPos;
	}

</script>

<script id="psIce" type="shader/fragment">

	#ifdef GL_ES
	precision highp float;
	#endif

	uniform samplerCube samplerR;
	uniform sampler2D samplerSuf;

	varying vec2 tc;
	varying vec3 tcCube;
	varying vec3 eyeDir;

	void main(void)
	{
		vec4 suf0 = texture2D(samplerSuf, tc);
		vec4 suf1 = texture2D(samplerSuf, tc*2.0);
		vec4 suf  = (suf0 + suf1)*0.5;

		vec3 eye = normalize(eyeDir);
		//vec3 nor = vec3(suf.x-0.5, 0.0, suf.z - 0.5);
		vec3 nor = vec3(0.0, 1.0, 0.0);
		nor = normalize(nor);
		float eDotN =  1.0 - dot(eye, nor);
		float fresnel = pow(eDotN, 7.5);
		vec4 refra = suf*0.2;
		vec3 refleCoord = reflect(-eye, nor);
		vec4 refle = textureCube(samplerR, refleCoord);
		vec4 sufColor = mix(refra, refle, fresnel);

		gl_FragColor = vec4(sufColor.xyz, 1.0);
	}

</script>


<!----------------------shader--------------------------->


<body onload="game();">
<canvas id = "webgl-canvas" ></canvas>
</body>
</html>
